package com.leezai.mqtt.dynamicSource;

import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.DataSourceWrapper;
import cn.hutool.setting.Setting;
import com.leezai.mqtt.utils.DynamicDbUtil;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;

public class DynamicDataSourceManager {
    private static final ConcurrentHashMap<String, DataSource> dataSourceMap = new ConcurrentHashMap<>();

    /**
     * 添加数据源
     */
    public static void addDataSource(String dsName, String url, String username,
                                     String password, String driver) {
        Setting setting = new Setting();
        setting.set("url", url);
        setting.set("username", username);
        setting.set("password", password);
        setting.set("driver", driver);

        // 根据驱动类型选择合适的连接池
        DSFactory dsFactory = DSFactory.create(setting);
        DataSource ds = dsFactory.getDataSource();
        dataSourceMap.put(dsName, ds);
    }


    /**
     * 获取数据源
     */
    public static DataSource getDataSource(String dsName) {
        return dataSourceMap.get(dsName);
    }

    /**
     * 移除数据源
     */
    public static void removeDataSource(String dsName) {
        DataSource ds = dataSourceMap.remove(dsName);
        if (ds != null) {
            closeDataSource(ds, dsName);
        } else {
            System.out.println("数据源 [" + dsName + "] 不存在");
        }
    }

    /**
     * 关闭数据源的通用方法
     */
    private static void closeDataSource(DataSource dataSource, String dsName) {
        try {
            // 输出数据源的实际类型
            System.out.println("数据源 [" + dsName + "] 的实际类型: " + dataSource.getClass().getName());


            if (dataSource instanceof DataSourceWrapper) {
                cn.hutool.db.ds.DataSourceWrapper wrapper =
                        (cn.hutool.db.ds.DataSourceWrapper) dataSource;
                wrapper.close();
            } else if (dataSource instanceof com.zaxxer.hikari.HikariDataSource) {
                com.zaxxer.hikari.HikariDataSource hikariDs =
                        (com.zaxxer.hikari.HikariDataSource) dataSource;
                hikariDs.close();
                System.out.println("Hikari数据源 [" + dsName + "] 已关闭");
                return;
            } else {
                System.out.println("数据源 [" + dsName + "] 类型不支持显式关闭");
            }

        } catch (Exception e) {
            System.err.println("关闭数据源 [" + dsName + "] 时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {
        String url = "jdbc:postgresql://8.134.103.174:5432/emergency2";
        String username = "postgres";
        String password = "123456Ljw!";
        String driver = "org.postgresql.Driver";
        addDataSource("ds1", url, username, password, driver);

        String url2 = "jdbc:postgresql://8.134.103.174:5432/emergency";
        String username2 = "postgres";
        String password2 = "123456Ljw!";
        String driver2 = "org.postgresql.Driver";
        addDataSource("ds2", url2, username2, password2, driver2);

        DataSource ds1 = dataSourceMap.get("ds1");
        Entity entity = DynamicDbUtil.queryOne("ds1", "select count(*) as number from czt_xx;");
        System.out.println(entity.getInt("number"));
        System.out.println(ds1.toString());

        DataSource ds2 = dataSourceMap.get("ds2");
        Entity entity2 = DynamicDbUtil.queryOne("ds2", "select * from czt_xx where gid = 2;");
        System.out.println(entity2.toString());
        System.out.println(ds2.toString());

        removeDataSource("ds1");
        removeDataSource("ds2");
    }
}
